package org.xs.object.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.xs.object.dao.class_table.MenuEntry;
import org.xs.object.service.IMenuConfigService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
public class MenuConfigService implements IMenuConfigService {
public ArrayList<Map<String, MenuEntry>> loadMenuConfig() {
    try {
        // 从类路径加载文件 src/main/resources/Class.json
        ClassPathResource resource = new ClassPathResource("Class.json");
        InputStream inputStream = resource.getInputStream();
        // 解析 JSON 到 Java 对象
        ObjectMapper objectMapper = new ObjectMapper();
        // 解析 JSON 到 Map
        Map<String, MenuEntry> menuMap=new HashMap<>();
        ArrayList<Map<String, MenuEntry>> menuList = objectMapper.readValue(
                inputStream,
                new TypeReference<ArrayList<Map<String, MenuEntry>>>() {}
        );

        return menuList;

    } catch (Exception e) {
        e.printStackTrace();
        return new ArrayList<>(); // 返回空Map而不是null
    }
}
}